package test.annotation.inherited;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import test.annotation.inherited.anno.IsInheritedAnnotation;
import test.annotation.inherited.anno.NoInherritedAnnotation;
import test.annotation.inherited.extend.MyInheritedClass;
import test.annotation.inherited.implement.IInheritedInterfaceChild;
import test.annotation.inherited.implement.MyInheritedClassUseInterface;
import test.design_pattern.strategy.SaleService;

import java.lang.annotation.Annotation;
import java.util.Arrays;

import static org.junit.Assert.assertEquals;

/**
 * 由于@RunWith和@SpringBootTest都包含了@Inherited，
 * 所以继承了InheritedTest 类的 InheritedTestChild 类就不用加这两个注解也能做单元测试
 */
public class InheritedTestChild extends InheritedTest{

    @Autowired
    SaleService saleService ;

    @Test
    public void testInheritedChild(){

        System.out.println(saleService.sale("vip",10));

        {
            /**
             * 子类会继承到超类的标注了@Inherited的注解，没有标注@Inherited的注解不会继承到
             */
            Annotation[] annotations = MyInheritedClass.class.getAnnotations();
            for (Annotation annotation : annotations) {
                System.out.println("MyInheritedClass----" + annotation);
            }
            assertEquals(true, Arrays.stream(annotations).anyMatch(l -> l.annotationType().equals(IsInheritedAnnotation.class)));
            assertEquals(true, Arrays.stream(annotations).noneMatch(l -> l.annotationType().equals(NoInherritedAnnotation.class)));
        }
        {
            /**
             * 接口继承不会传递@IsInherited注解
             */
            Annotation[] annotations = IInheritedInterfaceChild.class.getAnnotations();
            for (Annotation annotation : annotations) {
                System.out.println("IInheritedInterfaceChild----" + annotation);
            }
            assertEquals(true, Arrays.stream(annotations).noneMatch(l -> l.annotationType().equals(IsInheritedAnnotation.class)));
            assertEquals(true, Arrays.stream(annotations).noneMatch(l -> l.annotationType().equals(NoInherritedAnnotation.class)));
        }
        {
            /**
             * 接口实现也不会传递@IsInherited注解
             */
            Annotation[] annotations = MyInheritedClassUseInterface.class.getAnnotations();
            for (Annotation annotation : annotations) {
                System.out.println("MyInheritedClassUseInterface----" + annotation);
            }
            assertEquals(true, Arrays.stream(annotations).noneMatch(l -> l.annotationType().equals(IsInheritedAnnotation.class)));
            assertEquals(true, Arrays.stream(annotations).noneMatch(l -> l.annotationType().equals(NoInherritedAnnotation.class)));
        }

    }
}